//jsapi begin
var readyFunc = function onBridgeReady() {

	WeixinJSBridge.on('onWXDeviceBindStateChange', function(argv) {
		// console.log(JSON.stringify(argv));
	});
	WeixinJSBridge.on('onWXDeviceStateChange', function(argv) {
		// console.log(JSON.stringify(argv))
	});
	WeixinJSBridge.on('onScanWXDeviceResult', function(argv) {
		//alert(JSON.stringify(argv));
		// console.log(JSON.stringify(argv))
		if (argv.devices.length > 0) {
			if (!argv.devices[0].base64BroadcastData) {
				idList += argv.devices[0].deviceId + ";";
//				ajaxIsGGC();
			}
		}
	});
	WeixinJSBridge.on('onReceiveDataFromWXDevice', function(argv) {
		// console.log(JSON.stringify(argv))
	});
	//蓝牙状态监听
	WeixinJSBridge.on('onWXDeviceBluetoothStateChange', function(argv) {
		// console.log(JSON.stringify(argv))
	});

};

if (typeof WeixinJSBridge === "undefined") {
	document.addEventListener('WeixinJSBridgeReady', readyFunc, false);
} else {
	readyFunc();
}
/**
 * 初始化设备库
 * @return {[type]} [description]
 */
function openWXLib() {
	WeixinJSBridge.invoke('openWXDeviceLib', {}, function(res) {
		// console.log(JSON.stringify(res.err_msg));
		//alert(JSON.stringify(res.err_msg))
	});
}
/**
 * 关闭设备库
 * @return {[type]} [description]
 */
function closeWXLib() {
	WeixinJSBridge.invoke('closeWXDeviceLib', {}, function(res) {
		// console.log(JSON.stringify(res.err_msg));
	});
}
/**
 * 开始扫描
 * @return {[type]} [description]
 */
function startScanWXD() {
	WeixinJSBridge.invoke('startScanWXDevice', {}, function(res) {
		// console.log(JSON.stringify(res.err_msg));
//		alert(JSON.stringify(res.err_msg))
	});
}
/**
 * 停止扫描
 * @return {[type]} [description]
 */
function stopScanWXD() {
	WeixinJSBridge.invoke('stopScanWXDevice', {}, function(res) {
		// console.log(JSON.stringify(res.err_msg));
	});
}
//jsapi end

/**
 * 向服务器发送请求确认是否有扫描到公共秤设备
 * @return {[type]} [description]
 */
function ajaxIsGGC() {
	//停止搜索
	 stopScanWXD();
	 var openId = document.getElementById("openId").value;
	 if(idList){
		var xhr = createXmlHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var json = xhr.responseText;
					var jsonData = eval("(" + json + ")");
//					alert(JSON.stringify(jsonData));
					if (jsonData.code == 200) {
//						console.log(jsonData);
						
						//显示公共称按钮
						document.getElementById("gongongcheng").style.display = "block";
						document.getElementById("main").style.marginBottom = "40px";
						document.getElementById("deviceId").value = jsonData.deviceId;
						//15秒后隐藏按钮
						setTimeout(function(){
							document.getElementById("gongongcheng").style.display = "none";
						}, 30 * 60 * 1000);
						//
					}else{
						searchStart();
					} 
				}
			}
		};
		var url = ctx + "/ggc/ajax_isggc_servlet";
		xhr.open("post", url + "?openId="+ openId +"&idList=" + idList + "&clear=" + new Date() * 1, true);
		xhr.send();
	}else{
		searchStart();
	} 
}
//搜索到的设备ID集合
var idList = "";
//首页5秒后搜索周边设备

//搜素设备
function searchStart(){
	idList = "";
	closeWXLib();
	openWXLib();
	WeixinJSBridge.invoke('getWXDeviceInfos', {}, function(res){
// 		alert(JSON.stringify(res));
		for(var i = 0; i < res.deviceInfos.length; i++){
//			var deviceId = res.deviceInfos[i].deviceId;
			var state = res.deviceInfos[i].state;
	 		if(state == "connected" || state == "connecting"){
	 			idList += res.deviceInfos[i].deviceId + ";";
//	 			ajaxIsGGC();
	 		}
		}
	});
	
	setTimeout(function(){
		startScanWXD();
		//3秒后去请求服务器是否有ggc设备
		setTimeout(function(){
			ajaxIsGGC();
		},3000);
		
	}, 300);
	
}
/**
 * 去到公共称体验页面
 * @return {[type]} [description]
 */
function goGgcPage(){
	var openId = document.getElementById("openId").value;
	var memberId = document.getElementById("memberId").value;
	var deviceId = document.getElementById("deviceId").value;
	window.location.href = ctx + "/ggc/weightGGCIndex?openId=" + openId + "&memberId=" + memberId + "&type=weidongIndex&deviceId=" + deviceId + "&clear=" + new Date() * 1;
}